package com.burgeon.backend.Rest.view.data;
/**
 * 主页视图一
 */
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class MainData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public MainData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public HashMap getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        HashMap data = new HashMap();

        String str = RESULT(Data(), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        JSONArray jsonArray = null;

        jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows").getJSONArray(0);

        data.put("vipQty", jsonArray.getString(0));//总数量
        data.put("yAndQty", jsonArray.getString(1));//昨日新增
        data.put("yAndRate", String.format("%.0f", (jsonArray.getDouble(2) * 100))+"%");//昨日会员率
        data.put("tAndQty", jsonArray.getString(3));//今日新增
        data.put("tAndRate", String.format("%.0f", (jsonArray.getDouble(4) * 100))+"%");//今日会员率
        data.put("tmAndQty", jsonArray.getString(5));//本月新增
        data.put("tmAndRate", String.format("%.0f", (jsonArray.getDouble(6) * 100))+"%");//本月会员率
        data.put("lmAndQty", String.format("%.0f", (jsonArray.getDouble(7) * 100))+"%");//上月会员率
        data.put("lmAndRate", jsonArray.getString(8));//上月新增

        return data;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("TOT_VIP_QTY");//总数量
        columns.add("Y_QTY");//昨日新增
        columns.add("Y_RATE");//昨日会员率
        columns.add("T_QTY");//今日新增
        columns.add("N_RATE");//今日会员率
        columns.add("TM_QTY");//本月新增
        columns.add("TM_RATE");//本月会员率
        columns.add("LM_RATE");//上月会员率
        columns.add("LM_QTY");//上月新增

        params.put("columns", columns);
        params.put("table", "APP_VIPCUSTOT");//表名

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        MainData g = new MainData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
